package com.game.basics;

import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.Date;

import javax.swing.JFrame;

import com.game.graphics.Screen;

public class Component extends Applet implements Runnable
{
	private static final long serialVersionUID = 1L;

	/*********************************** BEGIN CONSTANTS *******************************************/
	public static final int WIDTH = 800;
	public static final int HEIGHT = 600;
	private static final int TICKS_PER_SECOND = 25;
	private static final int SKIP_TICKS = 1000 / TICKS_PER_SECOND;
	private static final int MAX_FRAMESKIP = 5;	
	private static final String TITLE = "Axis and Allies Pre-Alpha 0.01";
	private static final long startTime = new Date().getTime();
	/*********************************** END CONSTANTS *******************************************/

	/*********************************** BEGIN Global Variables *******************************************/
	private static boolean isRunning = false;

	public static Dimension size = new Dimension(WIDTH, HEIGHT);

	private static BufferedImage img;
	private static int[] pixels;
	private Thread thread;

	private static Screen screen;

	private float interpolation;

	/*********************************** END Global Variables *******************************************/

	public Component()
	{
		setPreferredSize(size);
	}

	private long GetTickCount()
	{
		long now = new Date().getTime();
		return (now - startTime);
	}

	public void init()
	{
		screen = new Screen(WIDTH, HEIGHT);
		img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
		pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
	}

	public void start()
	{
		isRunning = true;
		setPreferredSize(size);

		thread = new Thread(this);
		thread.start();
	}

	public void stop()
	{
		if (!isRunning)
			return;
		isRunning = false;
		try
		{
			thread.join();
		} catch (Exception e)
		{
			System.out.println(e.getStackTrace());
			System.exit(0);
		}
	}

	public static void main(String[] args)
	{
		Component component = new Component();
		JFrame frame = new JFrame();
		frame.add(component);
		frame.pack();
		frame.setTitle(TITLE);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);

		component.init();
		component.start();

	}

	public void run()
	{
		long next_game_tick = GetTickCount();
		int loops;
		while (isRunning)
		{
			loops = 0;
			while (GetTickCount() > next_game_tick && loops < MAX_FRAMESKIP)
			{
				// update_game();
				next_game_tick += SKIP_TICKS;
				loops++;
			}

			interpolation = (float) (GetTickCount() + SKIP_TICKS - next_game_tick) / (float) (SKIP_TICKS);
			display_game(interpolation);

		}
	}

	private void display_game(float num)
	{
		screen.render();

		for (int i = 0; i < WIDTH * HEIGHT; i++)
		{
			pixels[i] = screen.pixels[i];
		}

		Graphics g = this.getGraphics();
		g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);
		g.dispose();

	}

}
